package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.SearchService;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/list")
public class ListApiController {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    private SearchService searchService;


    /**
     搜索
     /api/list
    */
    @PostMapping
    public Result search(@RequestBody SearchParam searchchParam){

        //调用Service层中的方法 实现搜索的功能
        SearchResponseVo searchResponseVo = searchService.search(searchchParam);

        return Result.ok(searchResponseVo);
    }

    /**
     * api/list/inner/incrHotScore/{skuId}
     * 更新商品的热度排名

     */
    @GetMapping("/inner/incrHotScore/{skuId}")
    public Result incrHotScore(@PathVariable Long skuId){

        searchService.incrHotScore(skuId);

        return Result.ok();
    }


    /**
     * /api/list/inner/lowerGoods/{skuId}
     * 商品下架
     * @param skuId
     * @return
     */
    @GetMapping("/inner/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable Long skuId){
        searchService.lowerGoods(skuId);

        return Result.ok();

    }

    /**
     * /api/list/inner/upperGoods/{skuId}
     * 商品上架
     * @return
     */
    @GetMapping("/inner/upperGoods/{skuId}")
    public  Result upperGoods(@PathVariable Long skuId){

        searchService.upperGoods(skuId);


        return Result.ok();
    }

//    /**
//     * api/list/createIndex
//     * 创建索引库构建mapping结构
//     * @return
//     */
//    @GetMapping("/createIndex")
//    public Result createIndex(){
//        //创建索引库
//        elasticsearchRestTemplate.createIndex(Goods.class);
//        //创建mapping结构
//        elasticsearchRestTemplate.putMapping(Goods.class);
//
//
//        return Result.ok();
//    }

}
